<template>
  <div class="addAdapter">
    <div class="left flex direction">
      <h3>配置适配器</h3>
      <p style="font-size: 12px">* 请输入地址和端口进行配置</p>
      <div class="left-content">
        <el-form ref="form" label-width="80px" :model="form" size="mini">
          <el-form-item label="地址: ">
            <el-input v-model="form.address" spellcheck="false" />
          </el-form-item>
          <el-form-item label="端口: ">
            <el-input v-model="form.port" disabled />
          </el-form-item>
        </el-form>
        <el-button :loading="btnLoading" @click="newAdapters(form)"
          >确认设置</el-button
        >
      </div>
    </div>
    <div class="right flex direction">
      <h3>选择已有适配器</h3>
      <p style="font-size: 12px">* 可点击下方已有适配器进行快速设置</p>
      <el-table
        ref="tableData"
        v-loading="tableLoading"
        :cell-style="{ 'text-align': 'center' }"
        class="table-fixed"
        :data="adapterList"
        :header-cell-style="{ 'text-align': 'center', background: '#f5f5f5' }"
        height="800"
        style="width: 100%; cursor: pointer"
        @row-click="copyAdapter"
      >
        <el-table-column min-width="20%" type="expand">
          <template slot-scope="props">
            <el-form class="demo-table-expand" label-position="left">
              <el-form-item label="名称: ">
                <span>{{ props.row.name }}</span>
              </el-form-item>
              <el-form-item label="地址: ">
                <span>{{ props.row.address }}</span>
              </el-form-item>
              <el-form-item label="掩码: ">
                <span>{{ props.row.netmask }}</span>
              </el-form-item>
              <!-- <el-form-item label="Peer :">
                <span>{{ props.row.peer }}</span>
              </el-form-item> -->
              <el-form-item label="端口: ">
                <span>{{ props.row.port }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column label="ID" min-width="20%" type="index" />
        <el-table-column label="适配器名称" min-width="30%" prop="name" />
        <el-table-column label="适配器地址" min-width="30%" prop="address" />
      </el-table>
    </div>
  </div>
</template>

<script>
import adapter from "@/api/adapter";
export default {
  data() {
    return {
      form: {
        address: "",
        port: ""
      },
      adapterList: [],
      tableLoading: false,
      btnLoading: false
    };
  },
  created() {
    this.init();
  },
  methods: {
    async init() {
      // await this.getInfo();
      await this.getAddress();
      await this.getAdapterList();
    },
    async newAdapters() {
      this.btnLoading = true;
      await this.newAdapter();
    },
    // 页面展示权限获取
    getInfo() {
      return adapter.getInfo();
    },
    // 获取address和port
    getAddress() {
      return adapter.getAddress().then(res => {
        this.form.address = res.data.message.address;
        this.form.port = res.data.message.port;
      });
    },
    // 获取列表
    getAdapterList() {
      this.tableLoading = true;
      //let _this = this;
      return adapter.getAdapterList().then(res => {
        if (res.data.code == 200) {
          this.adapterList = res.data.adapters;
        } else {
          this.$message({
            message: res.data.message,
            type: "warning"
          });
        }
        this.tableLoading = false;
      });
    },
    // 设置address
    newAdapter() {
      this.$confirm("确认修改当前适配器配置吗？请再次确认", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        return adapter
          .newAdapter({
            address: this.form.address
          })
          .then(res => {
            if (res.data.code == 200) {
              this.$message({
                message: "设置成功！",
                type: "success"
              });
            } else {
              this.$message({
                message: res.data.message,
                type: "warning"
              });
            }
          });
      });
      this.btnLoading = false;
    },
    copyAdapter(row) {
      this.form.address = row.address;
      this.form.port = row.port;
      this.$message({
        message: "选择当前内容成功！",
        type: "success"
      });
    }
  }
};
</script>

<style lang="scss" scoped="scoped">
.addAdapter {
  display: flex;
  align-items: flex-start;
  // height: calc(100vh - 60px);
  height: 100%;
  padding: 15px 5%;

  h3 {
    font-weight: 700;
    margin-bottom: 50px;
  }
  p {
    margin-left: 10%;
    margin-bottom: 50px;
  }
  .left {
    top: 200px;
    display: flex;
    width: 50%;
    height: 100%;

    .left-content {
      display: flex;
      flex-direction: column;
      align-items: center;
      margin-left: 10%;

      .el-form {
        width: 100%;
      }

      .el-button {
        margin: 20px 0 0 80px;
        width: 30%;
      }
    }
  }

  .right {
    margin-left: 50px;
    display: flex;
    width: 50%;
    height: 500px;
    justify-content: center;

    .el-table {
      margin-left: 10%;
    }

    .demo-table-expand label {
      width: 90px;
      color: #99a9bf;
    }

    .el-form-item {
      margin-right: 0;
      margin-bottom: 0;
      width: 50%;
    }
  }
}
</style>
